/**
 * Created by cyh on 2016/1/9.
 */

$(document).ready(function () {
    //获取评论、喜欢
    $('.ds-thread').attr('data-url', window.location.href);
    var artcle_id = $('.ds-thread').attr('data-thread-key');
    $.getJSON('http://api.duoshuo.com/threads/counts.jsonp?short_name=zhusun&threads=' + artcle_id + '&callback=?', function (data) {
        if (data.code == 0) {
            var res = data.response[artcle_id];
            var comments = res['comments'];
            var likes = res['likes'];
            var repost = res['reposts'];
            $('#comments_count').text(comments);
            $('#like_count').text(likes);
            $('#repost_count').text(repost);
        }
    });

    var h_list = $("#art_content_div h1,h2");
    var last_ul = null;
    var last_li = null;
    var index_list = $('#index_list');

    for (var i = h_list.length - 1; i >= 0; i--) {
        var node = $(h_list[i]);
        var tagName = node.get(0).tagName;
        var text = node.text();
        if (tagName == "H1") {
            last_li = $("<li/>");
            if (last_ul == null) {
                var a = $("<a/>", {
                    href: "#" + text
                });
                a.text(text);
                last_li.prepend(a);
            }
            else {
                var a = $("<a/>", {
                    href: "#"
                });
                a.text(text);
                last_li.attr('class', 'uk-parent');
                last_li.prepend(last_ul);
                last_li.prepend(a);
            }
            index_list.after(last_li);
            last_ul = null;
        }
        else {
            if (last_ul == null) {
                last_ul = $("<ul/>", {
                    "class": "uk-nav-sub"
                });

            }
            var li = $("<li/>");
            var a = $("<a/>", {
                href: "#" + text
            });
            a.text(text);
            li.prepend(a);
            last_ul.prepend(li);

        }

    }

});

